const path = require('path');
const BaseIpcHandler = require('../BaseIpcHandler');

class GetPipeConfigHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> GetPipeConfigHandler params = `, params);
        const { name } = params;
        // console.log('GetPipeConfig name = ', name);

        // 业务逻辑
        let result = null;
        try {
            const list = require('../../pipeline/engine/configs/index.json');
            const pipe_conf = list.find((item)=>{
                return item.name === name;
            });
            const pipe_path = path.join('../../pipeline/engine/configs', pipe_conf.config);
            const pipe = require(pipe_path);
            result = { code: 0, msg: pipe };
        } catch (error) {
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        // console.log(`GetPipeConfigHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = GetPipeConfigHandler;
